package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.bean.Group;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.comman.Contants;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.util.JsonHelper;
import com.yuntongxun.ecdemo.voicechat.util.PreferenceUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 建群
 * Created by Administrator on 2016/11/28.
 */

public class CreateGroupActivity extends BaseChatActivity {
    @InjectView(R.id.group_name_edt)
    EditText nameEdt;
    @InjectView(R.id.group_introduction_edt)
    EditText introductionEdt;

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    public void onEvent(PostResult postResult){
        if (EventBusTags.CREATE_GROUP_SUCCESS.equals(postResult.getTag())){
            finish();
        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_create_group;
    }

    UserInfo userInfo;
    @Override
    protected void initViewsAndEvents() {
        setTitle("Group");
        String data= PreferenceUtils.getPrefString(this, Contants.Preference.user_info,"");
        if (!data.equals("")){
            JsonHelper<UserInfo> jsonHelper = new JsonHelper<UserInfo>(UserInfo.class);
            userInfo = jsonHelper.getData(data, "result");
        }

    }

    @OnClick(R.id.next_btn)
    public void onNextBtn(){

        if (TextUtils.isEmpty(nameEdt.getText().toString().trim())){
            showToastLong("Enter group name");
            return;
        }


        String result =PreferenceUtils.getPrefString(this, Contants.Preference.GROUP,"");
        Log.e("result",result);
        List<Group> groupList=parseResult(result);
        for(int i=0;i<groupList.size();i++){
            if (nameEdt.getText().toString().trim().equals(groupList.get(i).getName())){
                showToastLong("The group name already exists");
                return;
            }
        }

        if (TextUtils.isEmpty(introductionEdt.getText().toString().trim())){
            showToastLong("Enter group notice");
            return;
        }



        if (userInfo==null){
            return;
        }

        Intent intent=new Intent(this,ChoosePartnersActivity.class);
        intent.putExtra(ChoosePartnersActivity.ACCOUNT,userInfo.getAccountName());
        intent.putExtra(ChoosePartnersActivity.GROUP_NAME,nameEdt.getText().toString().trim());
        intent.putExtra(ChoosePartnersActivity.GROUP_INTROUDCTION,introductionEdt.getText().toString().trim());
        startActivity(intent);
    }

    public List<Group> parseResult(String result) {
        JsonHelper<Group> dataParser = new JsonHelper<Group>(
                Group.class);
        return dataParser.getDatas(
                result, "list");
    }


}
